<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 150px;
      height: 150px;
      background-color:aquamarine;
      position: relative;
      left: 0;
    }
  </style>
</head>
<body>
  <div class="box"></div>
  <script>
    // setTimeout
    // function timeout(delay = 1000) {
    //   return new Promise(resolve => {
    //     setTimeout(resolve, delay)
    //   })
    // }
    // timeout(2000).then(() => {
    //   console.log('setTimeout')
    //   return timeout(2000);
    // }).then(value => console.log('setTimeout2'))

    function interval(callback, delay = 200) {
      return new Promise(resolve => {
        let id = setInterval(() => {
          callback(id, resolve);
        }, delay)
      })
    }

    interval((timeId, resolve) => {
      const oBox = document.getElementsByClassName('box')[0]
      let left = parseInt(window.getComputedStyle(oBox).left);
      oBox.style.left = left + 10 + 'px';
      if (left >= 200) {
        clearInterval(timeId)
        resolve(oBox)
      }
    }).then(oBox => {
      console.log('fulfilled')
      return interval((timeId, resolve) => {
        let width = parseInt(window.getComputedStyle(oBox).width)
        oBox.style.width = width - 10 + 'px';
        if(width <= 20) {
          clearInterval(timeId)
          resolve(oBox)
        }
      }, 100)
    }).then(oBox => {
      oBox.style.backgroundColor = 'red'
    })
  </script>
</body>
</html>